<?php 
	class Habit_category_model extends CI_Model{

		function __construct(){
			parent::__construct();
		}

		function get_type_all(){
			$type_arr = array();

			$sql = "SELECT hc.*,(SELECT p.name from {$this->db->dbprefix}habit_category p where p.id = hc.pid) as parent_name FROM {$this->db->dbprefix}habit_category hc WHERE pid = 0 and status = 1 ORDER BY sort asc ";
			$result = $this->db->query($sql)->result_array();
			foreach ($result as $key => $value) {
				$type_arr[] = $value;
				$childs = $this->get_type_by_pid($value['id']);
				foreach ($childs as $key => $value) {
					$type_arr[] = $value;
				}
			}
			return $type_arr;
		}

		function get_type_by_pid($pid){
			$sql = "SELECT hc.*,(SELECT p.name from {$this->db->dbprefix}habit_category p where p.id = hc.pid) as parent_name FROM {$this->db->dbprefix}habit_category hc WHERE pid = $pid and status = 1 ORDER BY sort asc ";
			$result = $this->db->query($sql)->result_array();
			return $result;
		}
	}